/*
 * @(#)XfsimLogDialog.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsim.view;

import xfuzzy.lang.*;
import xfuzzy.util.*;
import xfuzzy.xfsim.model.*;
import xfuzzy.xfsim.view.listener.XfsimLogActionListener;
import javax.swing.*;
import java.awt.*;
import java.io.*;

/**
 * Di�logo para seleccionar el almacenamiento de los resultados
 * de simulaci�n en un archivo hist�rico
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfsimLogDialog extends JDialog {
	
	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603059L;
	
	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Descripci�n de la configuraci�n de almacenamiento
	 */
	private XfsimLog logfile;
	
	/**
	 * Selectores de variables a almacenar
	 */
	private JRadioButton radio[];
	
	/**
	 * Campo para seleccionar el fichero
	 */
	private XTextForm text;
	
	/**
	 * Fichero de almacenamiento seleccionado
	 */
	private File file;
	
	/**
	 * N�mero de variables de salida del sistema difuso
	 */
	private int outputlength;
	
	/**
	 * Resultado del di�logo
	 */
	private boolean result = false;
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfsimLogDialog(Xfsim xfsim, XfsimLog logfile) {
		super(xfsim,"Xfsim",true);
		
		this.logfile = logfile;
		this.file = logfile.getFile();
		build(xfsim);
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Informa si se ha selecionado el fichero o se ha cancelado
	 */
	public boolean getResult() {
		return result;
	}
		
	/**
	 *  Selecciona el fichero y las variables a almacenar
	 */
	public void set() {
		if(file == null) { Toolkit.getDefaultToolkit().beep(); return; }
		logfile.setFile(file);
		logfile.setNSelection(radio[0].isSelected());
		logfile.setTSelection(radio[1].isSelected());
		
		boolean[] oFlag = new boolean[outputlength];
		for(int i=0; i<outputlength; i++) oFlag[i] = radio[i+2].isSelected();
		logfile.setOutputSelection(oFlag);
		
		boolean[] iFlag = new boolean[radio.length-outputlength-2];
		for(int i=0; i<iFlag.length; i++) iFlag[i] = radio[i+outputlength+2].isSelected();
		logfile.setInputSelection(iFlag);
		
		result = true;
		setVisible(false);
	}
	
	/**
	 * Selecciona el fichero de almacenamiento
	 */
	public void file() {
		File wdir = (file == null? new File(System.getProperty("user.dir")) : file);
		XFileChooser chooser = new XFileChooser(wdir,XFileChooser.SELECT);
		chooser.setTitle("Select logfile");
		int val = chooser.showDialog();
		if(val == XFileChooser.CANCELED) return;
		file = chooser.getSelectedFile();
		text.setText(file.getAbsolutePath());
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Construye la ventana
	 */
	private void build(Xfsim xfsim) {
		XfsimLogActionListener listener = new XfsimLogActionListener(this);
		text = new XTextForm("File",listener);
		text.setActionCommand("File");
		text.setEditable(false);
		if(file != null) text.setText(file.getAbsolutePath());
		
		Variable input[] = xfsim.getSpecification().getSystemModule().getInputs();
		Variable output[] = xfsim.getSpecification().getSystemModule().getOutputs();
		this.outputlength = output.length;
		
		int length = 2+input.length+output.length;
		radio = new JRadioButton[length];
		radio[0] = new JRadioButton("Iteration");
		radio[0].setSelected(logfile.isNSelected());
		
		radio[1] = new JRadioButton("Time");
		radio[1].setSelected(logfile.isTSelected());
		
		boolean[] oFlag = logfile.getOutputSelection();
		for(int i=0; i<output.length; i++) {
			radio[i+2] = new JRadioButton(output[i].getName());
			if(oFlag!=null) radio[i+2].setSelected(oFlag[i]);
		}
		
		boolean[] iFlag = logfile.getInputSelection();
		for(int i=0; i<input.length; i++) {
			radio[i+outputlength+2] = new JRadioButton(input[i].getName());
			if(iFlag!=null) radio[i+outputlength+2].setSelected(iFlag[i]);
		}
				
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout( (radio.length+1)/2,2 ));
		for(int i=0; i<radio.length; i++) panel.add(radio[i]);
		Box panelbox = new Box(BoxLayout.X_AXIS);
		panelbox.add(Box.createHorizontalStrut(10));
		panelbox.add(Box.createHorizontalGlue());
		panelbox.add(panel);
		panelbox.add(Box.createHorizontalGlue());
		panelbox.add(Box.createHorizontalStrut(10));
		
		String lb[] = { "Set", "Cancel" };
		XCommandForm form = new XCommandForm(lb,lb,listener);
		form.setCommandWidth(100);
		
		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("Log file"));
		content.add(text);
		content.add(new XLabel("Variables to log"));
		content.add(panelbox);
		content.add(form);
		
		Point loc = xfsim.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		this.setLocation(loc);
		pack();		
	}

}
